package BookManage.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;


public class BaseDao {
        public static String driver;
        public static String url ;
        public  static String user;//用户名
        public  static String pwd;//密码

        public  Connection con = null;
        public PreparedStatement pstem=null;
        public ResultSet rs=null;

         static {
             init();
         }
         public static void init(){
             try {
                 Properties pro = new Properties();
                 String confing= "database.properties";
                 InputStream in= BaseDao.class.getClassLoader().getResourceAsStream(confing);
                 pro.load(in);
                 driver=pro.getProperty("driver");
                 url=pro.getProperty("url");
                 user=pro.getProperty("user");
                 pwd=pro.getProperty("pwd");
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }

        //1.获取连接方法
        public Connection getConnection() {
            Connection con = null;
            try {
                 Class.forName(driver);
                con = DriverManager.getConnection(url, user, pwd);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return con;
        }
        /**关闭连接方法
        rs ResultSet-对象
        pstmt PreparedStatement-对象
        con Connection-对象
        * */
        public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con) {
            try {

                if (null!=rs) {
                    rs.close();
                }
                if (null!=pstmt) {
                    pstmt.close();
                }
                if (null!=con) {
                    con.close();
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


        /**
         * 增删改操作
         */
        public int executeUpdate(String sql,Object[] params){
            int count = 0;
            PreparedStatement pstem=null;
            try {
                con=getConnection();//获取连接
               pstem = con.prepareStatement(sql);//创建pstem对象
                if (null!=params) {//如果数组不为空 表示sql中有 ？占位符
                    //没执行一次 就把数组中的值替换掉相应的 ？ 占位符
                    for (int i = 0; i <params.length ; i++) {
                        pstem.setObject((i+1),params[i]);
                    }
                }
                count=pstem.executeUpdate();//执行sql
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                this.closeAll(null,pstem,con);//关闭连接
            }

            return count;
        }
        public ResultSet executeQuery(String sql,Object[] params){

            ResultSet rs = null;
            PreparedStatement pstem=null;
            try {
                con=getConnection();//获取连接
                pstem = con.prepareStatement(sql);//创建pstem对象
                if (null!=params) {//如果数组不为空 表示sql中有 ？占位符
                    //没执行一次 就把数组中的值替换掉相应的 ？ 占位符
                    for (int i = 0; i <params.length ; i++) {
                        pstem.setObject((i+1),params[i]);
                    }
                }
                rs=pstem.executeQuery();//执行sql
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            return rs;
        }
    }
